package net.cardgame.orcalecard;

import jp.jma.oraclecard.MyApplication;
import jp.jma.oraclecard.R;
import net.cardgame.orcalecard.bean.DeckBean;
import net.cardgame.orcalecard.bean.SettingApp;
import net.cardgame.orcalecard.utils.ConstantValue;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.novoda.imageloader.core.ImageManager;
import com.novoda.imageloader.core.model.ImageTagFactory;

public class RandomActivity extends Activity implements OnClickListener,
		AnimationListener {

	ImageView img_i, img_title;
	int deckId;
	DeckBean deckBean;
	boolean flag = true;
	SettingApp setting;
	ImageView img_ring;
	int state_animation = 0;
	private ImageManager imageManager;
	private ImageTagFactory imageTagFactory;
	MediaPlayer mediaPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.random_activity_layout);
		img_i = (ImageView) findViewById(R.id.imageview);
		img_title = (ImageView) findViewById(R.id.imageview_title);
		img_ring = (ImageView) findViewById(R.id.img_ring);
		Bundle bundle = getIntent().getExtras();
		deckId = bundle.getInt("deckId");
		deckBean = new DeckBean();
		deckBean.deckId = deckId;
		deckBean.deckName = bundle.getString("deckName");
		deckBean.deckPrice = bundle.getInt("deckPrice");
		deckBean.pathServerDownload = bundle.getString("deckPathServer");
		deckBean.isUnlock = bundle.getBoolean("isUnlock");
		setting = ConstantValue.getSettingApp(this);
		if (deckId == 999) {
			img_title.setImageResource(R.drawable.c999n);
			img_i.setImageResource(R.drawable.c999i);
			img_i.setVisibility(View.VISIBLE);
			startAnimation();
		} else {
			String strDeckId = deckId < 10 ? "0" + deckId : "" + deckId;
			String strTitle = deckBean.pathServerDownload + "/c" + strDeckId
					+ "n.png";
			String strDeck = deckBean.pathServerDownload + "/c" + strDeckId
					+ "i.png";
			initImageLoader();
			setImageTag(img_i, strDeck);
			loadImage(img_i);
			setImageTag(img_title, strTitle);
			loadImage(img_title);
			img_i.setVisibility(View.VISIBLE);
			startAnimation();
		}
	}

	void startAnimation() {
		if (setting.sound) {
			// Play Music
			playMusic();
		}
		Animation deck_animation = AnimationUtils.loadAnimation(this,
				R.anim.random_deck_animation);
		deck_animation.setAnimationListener(this);
		img_i.startAnimation(deck_animation);
	}

	private void playMusic() {
		if (mediaPlayer == null) {
			mediaPlayer = MediaPlayer.create(this, R.raw.se_draw_card);
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mp.start();
				}
			});
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mp.release();
				}
			});

		}
		mediaPlayer.start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (!flag)
			return;
		else
			flag = false;
		if (deckId == 999
				&& (setting.listSpecial == null || setting.listSpecial
						.isEmpty())) {
			startActivityForResult(new Intent(RandomActivity.this,
					SettingSpecialActivity.class), 0);
			overridePendingTransition(R.anim.slide_in_right,
					R.anim.slide_out_left);
			RandomActivity.this.finish();
			return;
		}
		Intent intent = new Intent(RandomActivity.this, CardDeckActivity.class);
		intent.putExtras(getIntent().getExtras());
		startActivityForResult(intent, 0);
		overridePendingTransition(R.anim.activity_fade_in,
				R.anim.activity_fade_out);
		RandomActivity.this.finish();
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub
		switch (state_animation) {
		case 0:
			state_animation = 1;
			Animation ring_animation = AnimationUtils.loadAnimation(this,
					R.anim.random_ring);
			ring_animation.setAnimationListener(this);
			img_ring.setVisibility(View.VISIBLE);
			img_ring.startAnimation(ring_animation);
			//
			Animation title_animation = AnimationUtils.loadAnimation(this,
					R.anim.random_title_animation);
			img_title.setVisibility(View.VISIBLE);
			img_title.startAnimation(title_animation);
			break;
		case 1:
			findViewById(R.id.img_click).setOnClickListener(this);
			img_ring.setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	};

	private void initImageLoader() {
		imageManager = MyApplication.getInstance().getImageLoader();
		imageTagFactory = createImageTagFactory();
	}

	private ImageTagFactory createImageTagFactory() {
		ImageTagFactory imageTagFactory = ImageTagFactory.newInstance();
		imageTagFactory.setHeight(248);
		imageTagFactory.setWidth(188);
		imageTagFactory.setDefaultImageResId(R.drawable.transparent_image);
		imageTagFactory.setErrorImageId(R.drawable.no_image);
		imageTagFactory.setSaveThumbnail(true);
		return imageTagFactory;
	}

	private void setImageTag(ImageView view, String url) {
		view.setTag(imageTagFactory.build(url, this));
	}

	private void loadImage(ImageView view) {
		imageManager.getLoader().load(view);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, TopPageActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		super.onBackPressed();
	}
}
